//
//  ShellSysMessageView.m
//  VVBTC
//
//  Created by lvzhao on 2019/7/11.
//  Copyright © 2019  吕VV. All rights reserved.
//

#import "ShellSysMessageView.h"
#import "LZBaseTableView.h"
#import "ShellSysMessageViewModel.h"
#import "ShellSysMessageCell.h"


@interface ShellSysMessageView() <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) ShellSysMessageViewModel *viewModel;

@property (nonatomic,strong) LZBaseTableView *tableView;

@property (nonatomic,strong) NSMutableArray *foldArray;

@end

@implementation ShellSysMessageView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        self.viewModel = (ShellSysMessageViewModel*) viewModel;
        [self setupView];
    }
    return self;
}

#pragma mark - setupView
- (void)setupView{
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, K_SCREENWIDTH, 20)];
    headView.backgroundColor = COLOR_BACKGROUND;
    self.tableView.tableHeaderView = headView;
    
    @weakify(self);
    [self.tableView addHeadWithRefreshingBlock:^{
        @strongify(self);
        [self.viewModel getUserMessage:YES];
    }];
    
    [self.tableView addAutoNormalFooterWithRefreshingBlock:^{
        @strongify(self);
        [self.viewModel getUserMessage:NO];
    }];
    
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self reloadRecordData:[x integerValue]];
    }];
    
}

- (void)reloadRecordData:(NSInteger)type{
    self.tableView.mj_header.state  = MJRefreshStateIdle;
    self.tableView.mj_footer.state  = MJRefreshStateIdle;
    [self.tableView reloadData];
    [self.tableView dismissEmptyView];
    
    switch (type) {
        case 0:{
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
            break;
        default:
            break;
    }
    
    if(self.viewModel.dataArray.count == 0){
        self.tableView.mj_footer.hidden = YES;
        [self.tableView tableViewDisplayWithImgName:@"pic_nodata" message:LZLocalizedString(@"暂无数据") contentoffsetY:109 showEmptyView:YES];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShellSysMessageCell *messageCell = [tableView dequeueReusableCellWithIdentifier:@"ShellSysMessageCell"];
    
    if(self.viewModel.dataArray.count > indexPath.row){
        messageCell.modelObject = self.viewModel.dataArray[indexPath.row];
    }
    NSNumber *number = [NSNumber numberWithInteger:indexPath.row];
    messageCell.isFold = [self.foldArray containsObject:number];
    return messageCell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *number = [NSNumber numberWithInteger:indexPath.row];
    if([self.foldArray containsObject:number]){
        ShellSysMessageModel *messageModel = self.viewModel.dataArray[indexPath.row];
        return messageModel.cellHeight;
    } else {
        return 72;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *number = [NSNumber numberWithInteger:indexPath.row];
    
    if([self.foldArray containsObject:number]){
       [self.foldArray removeObject:number];
       
    } else {
       [self.viewModel updateMessageStatus:self.viewModel.dataArray[indexPath.row]];
       [self.foldArray addObject:number];
    }
    
    //刷线当前cell
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}


#pragma mark - actions
- (void)foldBtnClick:(UIButton *)sender{
    NSNumber *number = [NSNumber numberWithInteger:sender.tag];
    if([self.foldArray containsObject:number]){
        [self.foldArray removeObject:number];
        
    } else {
        
        [self.viewModel updateMessageStatus:self.viewModel.dataArray[sender.tag]];
        [self.foldArray addObject:number];
    }
    //刷线当前cell
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
#pragma mark - custool

#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_BACKGROUND;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ShellSysMessageCell class] forCellReuseIdentifier:@"ShellSysMessageCell"];
    }
    return _tableView;
}

- (NSMutableArray *)foldArray{
    if(!_foldArray){
        _foldArray = [[NSMutableArray alloc] init];
    }
    return _foldArray;
}

@end
